package popcorn.ticket.controller;

import java.net.InetAddress;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.entity.Logs;
import popcorn.ticket.entity.Ticket;
import popcorn.ticket.entity.Users;
import popcorn.ticket.service.TicketService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@Controller
public class TicketController {

	@Autowired
	private TicketService ts;

	Logger logger = Logger.getLogger(TicketController.class);

	// 查询所有+分页+影票编号查询
	@GetMapping(value = "tspage", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public PageData<Ticket> sltPage(
			@RequestParam(value = "ticketid", defaultValue = "0", required = false) int ticketid,
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "limit", defaultValue = "4", required = false) int limit,
			@RequestParam(value = "seldate", defaultValue = "", required = false) String seldate,
			@RequestParam(value = "selstatus", defaultValue = "4", required = false) int selstatus) {

		return ts.selectAll(ticketid, page, limit, seldate, selstatus);
	}

	// 生成影票
	@PostMapping(value = "addtk", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse addTicket(@RequestBody Ticket ticket) {

		return ts.insert(ticket);
	}

	// 修改取票状态或者显示状态
	@PostMapping(value = "updtk", consumes = "application/json", produces = "application/json;charset=utf-8")
	@ResponseBody
	public AjaxResponse updTicket(@RequestBody Ticket ticket) {

		return ts.updateDY(ticket);
	}

	// 查询单个用户的影票信息
	@RequestMapping("selTicketUser/{userid}")
	@ResponseBody
	public List<Ticket> selTicketByUser(@PathVariable("userid") int userid,
			@RequestParam(defaultValue = "1") int page) {
		PageHelper.startPage(page, 3);
		List<Ticket> list = ts.selectTicketByUserid(userid);
		return list;
	}

	@RequestMapping("countTicketUser/{userid}")
	@ResponseBody
	public int countTicketByUser(@PathVariable("userid") int userid) {
		List<Ticket> list = ts.selectTicketByUserid(userid);
		PageInfo<Ticket> pi = new PageInfo<Ticket>(list);
		return (int) pi.getTotal();
	}

	@RequestMapping(value = "delTicket", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public AjaxResponse delTicketByUser(int ticketid, int userid) {
		Logs logs = new Logs();
		logger.info("uid:"+userid);
		try {
			// 给log赋值
			logs.setLogip(InetAddress.getLocalHost() + "");
			logs.setLogobject("ticket");
			logs.setLogop("隐藏影票");
			logs.setRemark("用户删除影票信息");
			logs.setLogtime(new Date());
			Users u = new Users();
			// 这个要从前台获取信息在加入
			u.setUserid(ticketid);
			logs.setUsers(u);
			return ts.delTicketByUser(ticketid, logs);
		} catch (Exception e) {
			AjaxResponse ar = new AjaxResponse("删除失败", 0);
			return ar;
		}
	}

	@RequestMapping("getDoubleOrder/{userid}")
	@ResponseBody
	public List<Integer> getDoubleOrder(@PathVariable("userid") int userid) {
		return ts.judegeTicketNum(userid);
	}

}
